package com.bit.strongbaby;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {
	Button btn1;
	Button btn2;
	Button btn3;
	Button btn4;
	Button btn5;

	TextView txt1;
	TextView txt2;
	TextView txt3;

	WordDBHelper mHelper;
	SQLiteDatabase db;
	ContentValues row;
	String k;
	String p;
	String m;
	
	ImageView img1;
	public static long day = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		btn1 = (Button) findViewById(R.id.btn111);
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(),
						JoinActivity.class);
				startActivity(intent);
			}
		});
		
		btn2 = (Button) findViewById(R.id.gbtn2);
		btn2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				img1 = (ImageView)findViewById(R.id.imageView123);
				if(k!=null){
				Intent ini = new Intent(getBaseContext(), KeyActivity.class);
				startActivity(ini);
				}else if(k==null){
					Toast toast = Toast.makeText(getApplicationContext(), 
							"아이의 정보를 설정메뉴에서 입력해주세요!", 0);
					toast.show();	
				}
			}
		});

		btn3 = (Button) findViewById(R.id.gbtn3);
		btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				TabOne tab = new TabOne();
			if(k!=null){
				Intent intent = new Intent(getBaseContext(), GrowActivity.class);
				startActivity(intent);
			}else if(k==null){
				Toast toast = Toast.makeText(getApplicationContext(), 
						"아이의 정보를 설정메뉴에서 입력해주세요!", 0);
				toast.show();	
			}
			}
		});
		btn4 = (Button) findViewById(R.id.button4);
		btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			if(k!=null){
				Intent intent = new Intent(getBaseContext(), DataActivity.class);
				startActivity(intent);
			}else if(k==null){
				Toast toast = Toast.makeText(getApplicationContext(), 
						"아이의 정보를 설정메뉴에서 입력해주세요!", 0);
				toast.show();	
			}
			}
		});

		btn5 = (Button) findViewById(R.id.button5);
		btn5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			if(k!=null){
				Intent intent = new Intent(getBaseContext(), TalkActivity.class);
				startActivity(intent);
			}else if(k==null){
				Toast toast = Toast.makeText(getApplicationContext(), 
						"아이의 정보를 설정메뉴에서 입력해주세요!", 0);
				toast.show();	
			}
			}
		});
		
		mHelper = new WordDBHelper(this);
		db = mHelper.getWritableDatabase();
		Cursor cur;
		cur = db.rawQuery("select * from baby_info", null);

		String Result = "";
		while (cur.moveToNext()) {
			k = cur.getString(1);
			m = cur.getString(3);
			Result += (m);
		}
		cur.close();
		mHelper.close();

		DateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
		Date nowDate = new Date();
		String tempDate = sdFormat.format(nowDate);
		try {
			day = diffOfDate(Result, tempDate);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(k==null){
			txt3 = (TextView) findViewById(R.id.textView3);
			txt3.setText(" 우리아이의 정보를 설정메뉴에서 입력해주세요!");
			txt1 = (TextView) findViewById(R.id.textView1);
			txt1.setText("");
			txt2 = (TextView) findViewById(R.id.textView2);
			txt2.setText("");
		}else{
			txt3 = (TextView) findViewById(R.id.textView3);
			txt3.setText("");
			txt1 = (TextView) findViewById(R.id.textView1);
			txt1.setText(Long.toString(day));
			txt2 = (TextView) findViewById(R.id.textView2);
			txt2.setText("  "+k+"(이)가 태어난 지 D+");
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	public static long diffOfDate(String begin, String end) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		Date beginDate = formatter.parse(begin);
		Date endDate = formatter.parse(end);
		long diff = endDate.getTime() - beginDate.getTime();
		long diffDays = diff / (24 * 60 * 60 * 1000);
		return diffDays;
	}


}
